package com.stdio.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.stdio.R;
import com.stdio.modle.CallHistory;
import com.stdio.modle.CallHistoryImpl;
import com.stdio.util.timeformat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CallHistoryActivity extends Activity implements OnGestureListener,OnTouchListener{
	
	private LayoutInflater mInflater;
	private LinearLayout mContent;
	private LinearLayout mContent_list;
	//private LinearLayout mFlipperlayout;
	private ListView listview;
	private List<CallHistory> mCallData;
	private CallHistory mCallhistory;
	private CallHistoryImpl mCallhistoryimpl;
	private CallHistoryAdapter mAdapter;
	//private ViewFlipper mflipper;
	private GestureDetector mDetector;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.contact_pref);
		
		mInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		
		mDetector = new GestureDetector(this);
		mDetector.setIsLongpressEnabled(true);
		//this.setLongClickable(true);
		//this.setOnTouchListener(this);
		
		mContent = (LinearLayout)findViewById(R.id.content_lv);
		mContent_list = (LinearLayout)mInflater.inflate(R.layout.call_history, null);
		listview = (ListView)mContent_list.findViewById(R.id.callhistorylist);
		listview.setOnTouchListener(this);
		mContent.addView(mContent_list);
		initListView();
	}
	
	
	
	public void initFlipperviews()
	{
		
		//mflipper.addView(ContactsUI.);
	}
	
	private void initListView()
	{
		mAdapter = new CallHistoryAdapter(getListData(),this);
		listview.setAdapter(mAdapter);
		
	}
	
	private List<CallHistory> getListData()
	{
		mCallhistoryimpl = new CallHistoryImpl(this);
		mCallData = mCallhistoryimpl.getCallList();
		return mCallData;
	}
	
	class CallHistoryAdapter extends BaseAdapter
	{
		private List<CallHistory> mListData;
		private Context mContext;
		private int iCalltype;
		
		public CallHistoryAdapter(List<CallHistory> list,Context context)
		{
			this.mContext = context;
			this.mListData = list;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mListData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			CallHistoryView historyView = null;
			if(historyView == null)
			{
				historyView = new CallHistoryView();
				if(convertView == null)
				{
					convertView = (View)mInflater.inflate(R.layout.list_item_call_log, null);
				}
				
				historyView.callname = (TextView)convertView.findViewById(R.id.tv_name);
				//historyView.callphone = (TextView)convertView.findViewById(R.id.tv_all_count);
				historyView.callphone = (TextView)convertView.findViewById(R.id.tv_phone);
				//historyView.callloacation = (TextView)convertView.findViewById(R.id.tv_localtion);
				historyView.calldate = (TextView)convertView.findViewById(R.id.tv_date);
				historyView.calltype = (ImageView)convertView.findViewById(R.id.iv_type);
				historyView.calldial = (ImageView)convertView.findViewById(R.id.iv_dail);
				
				convertView.setTag(historyView);
			}
			else
			{
				historyView = (CallHistoryView)convertView.getTag();
			}
			
			mCallhistory = mListData.get(position);
			if(mCallhistory!=null)
			{
				historyView.callname.setText(mCallhistory.getName());
				historyView.callphone.setText(mCallhistory.getNumber());
				//Log.e("xxxxxxxxxxxxxx", mCallhistory.getCalldate().toLocaleString());
				  
				int flags = DateUtils.FORMAT_ABBREV_RELATIVE;
				
				historyView.calldate.setText(DateUtils.getRelativeTimeSpanString(mCallhistory.getCalldate().getTime(),
	                    System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS, flags));
				
				historyView.calldial.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_calllog_call));
				
				iCalltype = mCallhistory.getCalltype();
				switch(iCalltype)
				{
					case 1: historyView.calltype.setImageDrawable(mContext.getResources().
							getDrawable(R.drawable.ic_calllog_incomming_normal));
							break;
					case 2: historyView.calltype.setImageDrawable(mContext.getResources().
							getDrawable(R.drawable.ic_calllog_outgoing_nomal));
							break;
					case 3: historyView.calltype.setImageDrawable(mContext.getResources().
							getDrawable(R.drawable.ic_calllog_missed_normal));
							break;
					default:
				}
			}
			return convertView;
		}
		
	}
	class CallHistoryView
	{
		TextView callname;
		TextView callrecord;
		TextView callphone;
		TextView callloacation;
		TextView calldate;
		ImageView calltype;
		ImageView calldial;
	}
	@Override
	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		Log.e("bbbbbbbbbbb", "fafasdfsdf");
		return false;
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		
		Log.e("aaaaaaaaaaaaa", "fafasdfsdf");
		 if(e1.getX() > e2.getX()) 
		 {//move to left  
	           //mViewFlipper.showNext();
			   Intent intent = new Intent(CallHistoryActivity.this,ContactsUI.class);
			   startActivity(intent);
	           overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);  
	     }
		 else if(e1.getX() < e2.getX())
		 {    
			   Intent intent = new Intent(CallHistoryActivity.this,ContactsUI.class);
			   startActivity(intent);
	           overridePendingTransition(R.anim.push_left_in, R.anim.push_right_in);  
//	           mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in);  
//	           mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out);  
//	           mViewFlipper.showPrevious();  
//	           mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);  
//	           mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);  
	     }
		 else 
		 {  
	           return false;  
	     }  
	       
		 return true;  

	}
	@Override
	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent event) {
		// TODO Auto-generated method stub
		 Log.i("Fling", "Activity onTouchEvent!");  
		return this.mDetector.onTouchEvent(event);  
	}
	
//	public boolean onTouchEvent(MotionEvent event) {  
//       
//        return 
//    }  
	
}
